package com.huawei.Galenus.controller;

import com.alibaba.fastjson.JSONObject;
import com.huawei.Galenus.domain.Message;
import com.huawei.Galenus.domain.SSA;
import com.huawei.Galenus.service.FabricService;
import com.huawei.Galenus.service.SSAService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/ssa")
@Api("SSA相关接口")
public class SSAController {

    @Autowired
    private SSAService ssaService;

    @Autowired
    private FabricService fabricService;

    @ApiOperation("创建SSA")
    @PostMapping("/create")
    public Message create(@RequestParam("uid") String uid,
                          @RequestParam("name") String name,
                          @RequestParam("announcement") String announcement,
                          @RequestParam("description") String description) {
        String method = "ssa.create";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", uid);
        jsonObject.put("name", name);
        String[] params = new String[]{jsonObject.toJSONString()};
        JSONObject payload = fabricService.ssaInvoke(method, params);
        if (payload.getString("wrong_msg") == null) {
            SSA ssa = new SSA();
            ssa.setId(uid);
            ssa.setName(name);
            ssa.setAnnouncement(announcement);
            ssa.setDescription(description);
            ssaService.create(ssa);
            return new Message().ok(6666, "创建成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "创建失败").addData("payload", payload);
        }
    }

    @ApiOperation("展示所有SSA")
    @GetMapping("/getAll")
    public Message getAll() {
        List<SSA> ssaList = ssaService.getAllSSA();
        return new Message().ok(6666, "查询成功").addData("ssaList", ssaList);
    }

    @ApiOperation("获取某个用户创建的SSA")
    @GetMapping("/getOne")
    public Message getOne(@ApiParam("ssa ID") @RequestParam("ssaId") String ssaId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", ssaId);
        String[] params = new String[]{jsonObject.toJSONString()};
        String method = "ssa.query";
        JSONObject payload = fabricService.ssaQuery(method, params);
        if (payload.getString("wrong_msg") == null) {
            SSA ssa = ssaService.getById(ssaId);
            payload.put("announcement", ssa.getAnnouncement());
            payload.put("description", ssa.getDescription());
            return new Message().ok(6666, "查询成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "查询失败").addData("payload", payload);
        }
    }

    @ApiOperation("应援")
    @PutMapping("/support")
    public Message support(@RequestParam("uid")String uid, @RequestParam("ssaId")String ssaId,
                           @RequestParam("amount") int amount, @RequestParam("description") String description) {
        JSONObject jsonObject = new JSONObject();
        String id = UUID.randomUUID().toString();
        jsonObject.put("id", id);
        jsonObject.put("accountID", uid);
        jsonObject.put("ssaID", ssaId);
        jsonObject.put("amount", amount);
        jsonObject.put("description", description);
        String[] params = new String[]{jsonObject.toJSONString()};
        String method = "ssa.support";
        JSONObject payload = fabricService.ssaInvoke(method, params);
        if (payload.getString("wrong_msg") == null) {
            return new Message().ok(6666, "应援成功").addData("payload", payload);
        } else {
            return new Message().ok(1111, "应援失败").addData("payload", payload);
        }
    }
}
